package com.wy2.eclipse.tool.lib.git;

import java.util.List;
import org.eclipse.core.resources.WorkspaceJob;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;

public class GitJob extends WorkspaceJob {

    private final GitClient gitClient;
    private final List<GitRun> gitRuns;
    
    public GitJob(String name,GitClient gitClient,List<GitRun> gitRuns) {
        super(name);
        this.gitClient=gitClient;
        this.gitRuns=gitRuns;
    }

    @Override
    public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException {
        monitor.setCanceled(false);
        int runCount=gitRuns.size();
        monitor.beginTask(getName(), runCount);
        try {
            for (GitRun gitRun : gitRuns) {
                gitRun.run(gitClient);
                monitor.worked(1);
            }
            monitor.done();
        }catch (Exception e) {
            monitor.done();
            return new Status(IStatus.WARNING, "unknown", "", e);
        }
        return Status.OK_STATUS;
    }

}
